package com.wuh.controller;


import com.github.pagehelper.PageInfo;
import com.wuh.model.*;
import com.wuh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 公开课信息控制层
 */
@Controller
public class ClassInforController {
    private final String URL = "http://localhost:8080/";

    @Autowired
    private ClassInforService classInforService;

    @Autowired
    private ClassTypeService classTypeService;

    @Autowired
    public ClassAttachmentService classAttachmentService;

    @Autowired
    private TeacherCommentService teacherCommentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ClassSignUpService classSignUpService;

    /**
     * 添加公开课
     *
     * @param classInfor
     * @return
     */
    @RequestMapping("/addClassInfor")
    public String addClassInfor(MultipartFile upImage, ClassInfor classInfor) {
        if (!upImage.isEmpty()) {
            //获取项目文件地址
            String filepath = new String("src/main/resources/" + "static/classimage");
            File staticFile = new File(filepath);
            //设置文件名
            String fileName = upImage.getOriginalFilename();  //获取原文件名
            String uuid = UUID.randomUUID().toString().replace("-", ""); //把文件的名称设置唯一值，uuid
            fileName = uuid + "_" + fileName; //文件名
            //图片保存全路径
            String uploadPath = staticFile.getAbsolutePath() + File.separator + fileName;
            File targetFile = new File(uploadPath);
            //上传文件

            if (!targetFile.exists()) {
                targetFile.mkdirs();
                System.out.println("创建新文件夹");
            } else {
                System.out.println("文件夹存在");
            }
            try {
                upImage.transferTo(targetFile);  //上传
                System.out.println("上传成功！");
            } catch (IOException e) {
                e.printStackTrace();
            }
            //存储图片访问地址
            fileName = "/classimage\\" + fileName;
            //存储宣传图片地址
            classInfor.setImgUrl(fileName);
            classInforService.addClassInfo(classInfor);
            return "redirect:/queryClassInforAll";
        }
        classInforService.addClassInfo(classInfor);
        return "redirect:/queryClassInforAll";
    }


    /**
     * 跳转我的公开课信息页面，加载公开课展示，预加载公开课类别
     *
     * @param classInfor 教师填写的公开课信息
     * @return 返回数据和视图url
     */
    @RequestMapping("/queryClassInforAll")
    public String queryClassInforAll(ClassInfor classInfor, Model model, HttpSession session) {
        //加载我发布的公开课数据
        PageInfo<ClassInfor> classInforPageInfo = classInforService.queryClassInforAll(classInfor);
        System.err.println("classInforPageInfo====" + classInforPageInfo);
        model.addAttribute("classInforPageInfo", classInforPageInfo);
        //加载公开课类别数据
        ClassType classType = new ClassType();
        List<ClassType> classTypes = classTypeService.queryClassTypeAll(classType);
        System.out.println("预加载的数据classTypes:" + classTypes);
        session.setAttribute("classTypes", classTypes);
        //加载最新公开课数据
        PageInfo<ClassInfor> newClassInfor = classInforService.queryClassAllByDate(classInfor);
//        System.err.println("加载的最新公开课newClassInfor：" + newClassInfor);
        session.setAttribute("newClassInfor", newClassInfor);
        return "openclass/tercher_myclass";
    }

    /**
     * 管理员之公开课
     *
     * @param classInfor 查询条件
     * @return 视图
     */
    @RequestMapping("/queryClassInforAllAdmin")
    public String queryClassInforAllAdmin(ClassInfor classInfor, Model model, HttpSession session) {
        ClassType classType = new ClassType();
        List<ClassType> classTypes = classTypeService.queryClassTypeAll(classType);
        //预加载课程类别
        session.setAttribute("classTypes", classTypes);
        //预加载老师信息
        List<Teacher> teachers = teacherService.schTeachers();
        session.setAttribute("teachers", teachers);
        //加载公开课数据
        PageInfo<ClassInfor> classInforPageInfos = classInforService.queryClassInforAll(classInfor);
//        System.err.println("classInforPageInfos====" + classInforPageInfos);
        model.addAttribute("classInforPageInfos", classInforPageInfos);
        return "openclass/admin_openclass";
    }


    /**
     * 修改公开课信息
     *
     * @param classInfor
     * @return
     */
    @RequestMapping("/updateClassInfor")
    public String updateClassInfor(MultipartFile upImage, ClassInfor classInfor) {
        System.out.println("修改接收到的ClassInfor：" + classInfor);
        if (!upImage.isEmpty()) {
            //获取项目文件地址
            String filepath = new String("src/main/resources/" + "static/classimage");
            File staticFile = new File(filepath);
            //设置文件名
            String fileName = upImage.getOriginalFilename();  //获取原文件名
            String uuid = UUID.randomUUID().toString().replace("-", ""); //把文件的名称设置唯一值，uuid
            fileName = uuid + "_" + fileName; //文件名
            //图片保存全路径
            String uploadPath = staticFile.getAbsolutePath() + File.separator + fileName;
            File targetFile = new File(uploadPath);
            //上传文件

            if (!targetFile.exists()) {
                targetFile.mkdirs();
                System.out.println("创建新文件夹");
            } else {
                System.out.println("文件夹存在");
            }
            try {
                upImage.transferTo(targetFile);  //上传
                System.out.println("上传成功！");
            } catch (IOException e) {
                e.printStackTrace();
            }
            //存储图片访问地址
            fileName = "/classimage\\" + fileName;
            //存储宣传图片地址
            classInfor.setImgUrl(fileName);
            classInforService.updateClassInfo(classInfor);

        } else {
            classInforService.updateClassInfo(classInfor);
        }
        return "redirect:/queryClassInforAll";
    }

    /**
     * 删除公开课
     *
     * @param classId
     * @return
     */
    @RequestMapping("/deleteClassInfo")
    public String deleteClassInfo(Integer[] classId) {
        for (Integer integer : classId) {
            classSignUpService.deleteById(integer);
        }
        classAttachmentService.deleteById(classId);
        classInforService.deleteById(classId);
        return "redirect:/queryClassInforAll";
    }

    /**
     * 管理员删除公开课
     *
     * @param classId
     * @return
     */
    @RequestMapping("/deleteClassInfos")
    public String deleteClassInfos(Integer[] classId) {
        for (Integer integer : classId) {
            classSignUpService.deleteById(integer);
        }
        classAttachmentService.deleteById(classId);
        classInforService.deleteById(classId);
        return "redirect:/queryClassInforAllAdmin";
    }

    @RequestMapping("/closeClassInfo")
    public String closeClassInfo(Integer[] classId) {
        System.out.println("closeClassInfo执行了");
        ClassInfor classInfor = new ClassInfor();
        for (Integer integer : classId) {
            classInfor.setClassId(integer);
            classInfor.setStatus(2);
            classInforService.updateClassInfo(classInfor);
        }
        return "redirect:/queryClassInforAllAdmin";
    }

    @RequestMapping("/getOkClassInfo")
    public String getOkClassInfo(Integer[] classId) {
        System.out.println("getOkClassInfo执行了");
        ClassInfor classInfor = new ClassInfor();
        for (Integer integer : classId) {
            classInfor.setClassId(integer);
            classInfor.setStatus(1);
            classInforService.updateClassInfo(classInfor);
        }
        return "redirect:/queryClassInforAllAdmin";
    }


    /**
     * 跳转我的公开课预加载课程类别
     *
     * @return
     */
    @RequestMapping("/toTeacherMyClass")
    public String toTeacherMyClass(HttpSession session) {
        ClassType classType = new ClassType();
        List<ClassType> classTypes = classTypeService.queryClassTypeAll(classType);
//        System.out.println("预加载的数据classTypes:" + classTypes);
        session.setAttribute("classTypes", classTypes);
        return "openclass/tercher_myclass";
    }

    /**
     * 跳转最新公开课
     *
     * @return
     */
    @RequestMapping("/toNewClass")
    public String toNewClass(HttpSession session) {

        return "openclass/newclass";
    }

    /**
     * 跳转最新公开课
     *
     * @return
     */
    @RequestMapping("/newClass")
    public void newClass(HttpSession session) {
        ClassInfor classInfor = new ClassInfor();
        //加载最新公开课数据
        PageInfo<ClassInfor> newClassInfor = classInforService.queryClassAllByDate(classInfor);
//        System.err.println("加载的最新公开课newClassInfor：" + newClassInfor);
        session.setAttribute("newClassInfor", newClassInfor);
    }

    /**
     * 跳转导航栏公开课
     *
     * @return
     */
    @RequestMapping("/toClassMain")
    public String toClassMain() {
        return "openclass/class_main";
    }

    /**
     * 跳转名师推荐
     *
     * @return
     */
    @RequestMapping("/toTeacherRd")
    public String toTeacherRd() {
        return "openclass/teacher_recommended";
    }

    /**
     * 跳转发布公开课页面
     *
     * @return
     */
    @RequestMapping("/toClassRelease")
    public String toClassRelease(Model model) {
        ClassInfor classInfor = new ClassInfor();
        PageInfo<ClassInfor> classInforPageInfo = classInforService.queryClassInforAll(classInfor);
        System.err.println("classInforPageInfo====" + classInforPageInfo);
        model.addAttribute("classInforPageInfo", classInforPageInfo);
        return "openclass/class_release";
    }

    /**
     * 编辑公开课页面预加载数据
     *
     * @return
     */
    @RequestMapping("/toClassUpdate")
    public String toClassUpdate(Integer classId, Model model) {
        //修改数据预加载
        ClassInfor classInfor = classInforService.queryById(classId);
//        System.out.println("返回的数据：" + classInfor);
        model.addAttribute("updateClassInfor", classInfor);
        return "openclass/class_update";
    }

    /**
     * 跳转发课程明细页面,并且显示对应教师的数据
     *
     * @return
     */
    @RequestMapping("/toClassDetails")
    public String toClassDetails(Integer classId, Integer teacher_id, HttpSession session) {
        ClassInfor classInfor = classInforService.queryById(classId);
        System.out.println("课程明细classInfor:" + classInfor);
        session.setAttribute("classInforDetails", classInfor);
        //加载教师评价信息
        TeacherComment teacherComment = new TeacherComment();
        teacherComment.setTeacherId(teacher_id);
        List<TeacherComment> teacherComments = teacherCommentService.queryCommentAll(teacherComment);
        session.setAttribute("teacherComments", teacherComments);
        return "openclass/class_details";
    }

    /**
     * 跳转课程介绍页面
     *
     * @return
     */
    @RequestMapping("/toClassDescription")
    public String toClassDescription() {
        return "openclass/class_description";
    }

    /**
     * 跳转课程介绍页面
     *
     * @return
     */
    @RequestMapping("/toTeacherDetails")
    public String toTeacherDetails(Integer classId, Integer teacher_id, HttpSession session) {
        ClassInfor classInfor = classInforService.queryById(classId);
        System.out.println("课程明细classInfor:" + classInfor);
        session.setAttribute("classInforDetails", classInfor);
        //加载教师评价信息
        TeacherComment teacherComment = new TeacherComment();
        teacherComment.setTeacherId(teacher_id);
        List<TeacherComment> teacherComments = teacherCommentService.queryCommentAll(teacherComment);
        session.setAttribute("teacherComments", teacherComments);
        return "openclass/teacher_details";
    }

    @RequestMapping("/toTeacherDetails1")
    public String toTeacherDetails1() {
        return "openclass/teacher_details";
    }


}
